package com.yisu.expressway.ui.recyclerview;

import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;

/**
 * Created by cundong on 2015/10/9.
 * <p/>
 * 拓展的StaggeredGridLayoutManager，tks @Jack Tony
 */
public class ExStaggeredGridLayoutManager extends StaggeredGridLayoutManager {

    private final String TAG = getClass().getSimpleName();

    GridLayoutManager.SpanSizeLookup mSpanSizeLookup;

    public ExStaggeredGridLayoutManager(int spanCount, int orientation) {
        super(spanCount, orientation);
    }

    /**
     * Returns the current used by the GridLayoutManager.
     *
     * @return The current used by the GridLayoutManager.
     */
    public GridLayoutManager.SpanSizeLookup getSpanSizeLookup() {
        return mSpanSizeLookup;
    }

    /**
     * 设置某个位置的item的跨列程度，这里和GridLayoutManager有点不一样，
     * 如果你设置某个位置的item的span>1了，那么这个item会占据所有列
     *
     * @param spanSizeLookup instance to be used to query number of spans
     *                       occupied by each item
     */
    public void setSpanSizeLookup(GridLayoutManager.SpanSizeLookup spanSizeLookup) {
        mSpanSizeLookup = spanSizeLookup;
    }

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
        //Log.d(TAG, "item count = " + getItemCount());
		
		if (mSpanSizeLookup != null){
	        for (int i = 0; i < getItemCount(); i++) {
	            if (mSpanSizeLookup.getSpanSize(i) > 1) {
	                //Log.d(TAG, "lookup > 1 = " + i);
	                try {
	                    //fix 动态添加时报IndexOutOfBoundsException
	                    View view = recycler.getViewForPosition(i);
	                    if (view != null) {
	                        /**
	                         *占用所有的列
	                         * @see https://plus.google.com/+EtienneLawlor/posts/c5T7fu9ujqi
	                         */
	                        LayoutParams lp = (LayoutParams) view.getLayoutParams();
	                        lp.setFullSpan(true);
	                    }
	                    // recycler.recycleView(view);
	                } catch (Exception e) {
	//                    e.printStackTrace();
	                }
	            }
	        }
		}
        super.onMeasure(recycler, state, widthSpec, heightSpec);
    }


    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            return super.scrollVerticallyBy(dy, recycler, state);
        }catch (IndexOutOfBoundsException ex){

        }
        return 0;
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(RecyclerView.Recycler recycler, RecyclerView.State state, AccessibilityNodeInfoCompat info) {
        try {
            super.onInitializeAccessibilityNodeInfo(recycler, state, info);
        }catch (final NullPointerException e){

        }
    }

    @Override
    public void onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler recycler, RecyclerView.State state, View host, AccessibilityNodeInfoCompat info) {
        try {
            super.onInitializeAccessibilityNodeInfoForItem(recycler, state, host, info);
        }catch (final NullPointerException e){

        }
    }

    @Override
    public void onInitializeAccessibilityEvent(final AccessibilityEvent event) {
        try {
            super.onInitializeAccessibilityEvent(event);
        } catch (final NullPointerException e) {
//            Log.e(TAG, "NullPointerException in onInitializeAccessibilityEvent(" + event +
//                    ')', e);
        }
    }
    @Override
    public void onInitializeAccessibilityEvent(final RecyclerView.Recycler recycler,
                                               final RecyclerView.State state, final AccessibilityEvent event) {
        try {
            super.onInitializeAccessibilityEvent(recycler, state, event);
        } catch (final NullPointerException e) {
//            Log.e(TAG, "NullPointerException in onInitializeAccessibilityEvent(" + recycler +
//                    ", " + state + ", " + event + ')', e);
        }
    }
}